######################################################################################
## Institutional Convergence in Europe								##
## Nina Sch�nfelder and Helmut Wagner								##
##  														##
## Program code to generate figures and estimate convergence in Section 4 		##
## Sigma and unconditional beta convergence							##
## Worldwide Governance Indicators									##
## R version 3.2.2 (2015-08-14) on platform: x86_64-w64-mingw32/x64 (64-bit)		##
######################################################################################

######################################################################################
### Read and process data										##
######################################################################################


### WGIs 

## Original Excel-file is converted to cvs-files previously.
WGI_VaA<-read.csv2(file="wgi_VaA_2013.csv", header = FALSE, na.strings = c("#NV"),skip = 13,nrows=300,stringsAsFactors=FALSE)
WGI_VaA<-as.matrix(WGI_VaA[,-2])
WGI_VaA[2,]<-paste( WGI_VaA[2,],WGI_VaA[1,],sep=".")
colnames(WGI_VaA) <- WGI_VaA[2,]
WGI_VaA<-WGI_VaA[-c(1:2),]
WGI_VaA<-as.data.frame(WGI_VaA)
# Delete unnecessary  variables StdErr,NumSrc, Rank, Lower, Upper)
k<-seq.int(from=2, to=85, by=6)
WGI_VaA<-WGI_VaA[,c(1,k)]
## Write and read table als data.frame
write.csv2(WGI_VaA, file = "WGI_VaA_wide_2013.csv",quote = FALSE,row.names = FALSE)
WGI_VaA<-read.csv2(file="WGI_VaA_wide_2013.csv",header=TRUE,row.names = NULL)
colnames(WGI_VaA)[1]<-"Country"
## Done for WGI_VaA. ###
## Do the same for CoC, GE, PSNV, RoL, RQ.


WGI_CoC<-read.csv2(file="wgi_CoC_2013.csv", header = FALSE, na.strings = c("#NV"),skip = 13,nrows=300,stringsAsFactors=FALSE)
WGI_CoC<-as.matrix(WGI_CoC[,-2])
WGI_CoC[2,]<-paste( WGI_CoC[2,],WGI_CoC[1,],sep=".")
colnames(WGI_CoC) <- WGI_CoC[2,]
WGI_CoC<-WGI_CoC[-c(1:2),]
WGI_CoC<-as.data.frame(WGI_CoC)
k<-seq.int(from=2, to=85, by=6)
WGI_CoC<-WGI_CoC[,c(1,k)]
write.csv2(WGI_CoC, file = "WGI_CoC_wide_2013.csv",quote = FALSE,row.names = FALSE)
WGI_CoC<-read.csv2(file="WGI_CoC_wide_2013.csv",header=TRUE,row.names = NULL)
colnames(WGI_CoC)[1]<-"Country"
## Done for WGI_CoC

WGI_GE<-read.csv2(file="wgi_GE_2013.csv", header = FALSE, na.strings = c("#NV"),skip = 13,nrows=300,stringsAsFactors=FALSE)
WGI_GE<-as.matrix(WGI_GE[,-2])
WGI_GE[2,]<-paste( WGI_GE[2,],WGI_GE[1,],sep=".")
colnames(WGI_GE) <- WGI_GE[2,]
WGI_GE<-WGI_GE[-c(1:2),]
WGI_GE<-as.data.frame(WGI_GE)
k<-seq.int(from=2, to=85, by=6)
WGI_GE<-WGI_GE[,c(1,k)]
write.csv2(WGI_GE, file = "WGI_GE_wide_2013.csv",quote = FALSE,row.names = FALSE)
WGI_GE<-read.csv2(file="WGI_GE_wide_2013.csv",header=TRUE,row.names = NULL)
colnames(WGI_GE)[1]<-"Country"
## Done for WGI_GE

WGI_PSNV<-read.csv2(file="wgi_PSNV_2013.csv", header = FALSE, na.strings = c("#NV"),skip = 13,nrows=300,stringsAsFactors=FALSE)
WGI_PSNV<-as.matrix(WGI_PSNV[,-2])
WGI_PSNV[2,]<-paste( WGI_PSNV[2,],WGI_PSNV[1,],sep=".")
colnames(WGI_PSNV) <- WGI_PSNV[2,]
WGI_PSNV<-WGI_PSNV[-c(1:2),]
WGI_PSNV<-as.data.frame(WGI_PSNV)
k<-seq.int(from=2, to=85, by=6)
WGI_PSNV<-WGI_PSNV[,c(1,k)]
write.csv2(WGI_PSNV, file = "WGI_PSNV_wide_2013.csv",quote = FALSE,row.names = FALSE)
WGI_PSNV<-read.csv2(file="WGI_PSNV_wide_2013.csv",header=TRUE,row.names = NULL)
colnames(WGI_PSNV)[1]<-"Country"
## Done for WGI_PSNV

WGI_RoL<-read.csv2(file="wgi_RoL_2013.csv", header = FALSE, na.strings = c("#NV"),skip = 13,nrows=300,stringsAsFactors=FALSE)
WGI_RoL<-as.matrix(WGI_RoL[,-2])
WGI_RoL[2,]<-paste( WGI_RoL[2,],WGI_RoL[1,],sep=".")
colnames(WGI_RoL) <- WGI_RoL[2,]
WGI_RoL<-WGI_RoL[-c(1:2),]
WGI_RoL<-as.data.frame(WGI_RoL)
k<-seq.int(from=2, to=85, by=6)
WGI_RoL<-WGI_RoL[,c(1,k)]
write.csv2(WGI_RoL, file = "WGI_RoL_wide_2013.csv",quote = FALSE,row.names = FALSE)
WGI_RoL<-read.csv2(file="WGI_RoL_wide_2013.csv",header=TRUE,row.names = NULL)
colnames(WGI_RoL)[1]<-"Country"
## Done for WGI_RoL

WGI_RQ<-read.csv2(file="wgi_RQ_2013.csv", header = FALSE, na.strings = c("#NV"),skip = 13,nrows=300,stringsAsFactors=FALSE)
WGI_RQ<-as.matrix(WGI_RQ[,-2])
WGI_RQ[2,]<-paste( WGI_RQ[2,],WGI_RQ[1,],sep=".")
colnames(WGI_RQ) <- WGI_RQ[2,]
WGI_RQ<-WGI_RQ[-c(1:2),]
WGI_RQ<-as.data.frame(WGI_RQ)
k<-seq.int(from=2, to=85, by=6)
WGI_RQ<-WGI_RQ[,c(1,k)]
write.csv2(WGI_RQ, file = "WGI_RQ_wide_2013.csv",quote = FALSE,row.names = FALSE)
WGI_RQ<-read.csv2(file="WGI_RQ_wide_2013.csv",header=TRUE,row.names = NULL)
colnames(WGI_RQ)[1]<-"Country"
## Done for WGI_RQ

## Make list of all WGI-indicator data-frames.
WGI<-list(VaA=WGI_VaA,CoC=WGI_CoC,GE=WGI_GE,PSNV=WGI_PSNV,RoL=WGI_RoL,RQ=WGI_RQ)



### Make country groups


## Select countries which were at least potential candidate countries to the EU.
countries<-c("BELGIUM","GERMANY","ESTONIA","IRELAND","GREECE","SPAIN","FRANCE","ITALY",
	"CYPRUS","LUXEMBOURG","MALTA","NETHERLANDS","AUSTRIA","PORTUGAL","SLOVENIA",
	"SLOVAK REPUBLIC","FINLAND","BULGARIA","CZECH REPUBLIC","DENMARK","LATVIA","LITHUANIA",
	"HUNGARY","POLAND","ROMANIA","SWEDEN","UNITED KINGDOM","CROATIA","MACEDONIA, FYR",
	"ICELAND","MONTENEGRO","SERBIA","TURKEY","ALBANIA", "BOSNIA AND HERZEGOVINA", "KOSOVO")

WGI<-lapply(WGI, FUN=subset, subset= Country %in% countries)

## Factors may have empty levels after subsetting; unused levels are to remove.
WGI<-lapply(WGI,droplevels)

EUaffiliates<-c("BELGIUM","GERMANY","ESTONIA","IRELAND","GREECE","SPAIN","FRANCE","ITALY",
	"CYPRUS","LUXEMBOURG","MALTA","NETHERLANDS","AUSTRIA","PORTUGAL","SLOVENIA",
	"SLOVAK REPUBLIC","FINLAND","BULGARIA","CZECH REPUBLIC","DENMARK","LATVIA","LITHUANIA",
	"HUNGARY","POLAND","ROMANIA","SWEDEN","UNITED KINGDOM","CROATIA","MACEDONIA, FYR",
	"ICELAND","SERBIA","TURKEY","ALBANIA", "BOSNIA AND HERZEGOVINA")  	
	# without "MONTENEGRO" and "KOSOVO" as data for the frist years is missing.
EU27<-c("BELGIUM","GERMANY","ESTONIA","IRELAND","GREECE","SPAIN","FRANCE","ITALY",
	"CYPRUS","LUXEMBOURG","MALTA","NETHERLANDS","AUSTRIA","PORTUGAL","SLOVENIA",
	"SLOVAK REPUBLIC","FINLAND","BULGARIA","CZECH REPUBLIC","DENMARK","LATVIA","LITHUANIA",
	"HUNGARY","POLAND","ROMANIA","SWEDEN","UNITED KINGDOM")
EA17<-c("BELGIUM","GERMANY","ESTONIA","IRELAND","GREECE","SPAIN","FRANCE","ITALY",
	"CYPRUS","LUXEMBOURG","MALTA","NETHERLANDS","AUSTRIA","PORTUGAL","SLOVENIA",
	"SLOVAK REPUBLIC","FINLAND")

EA12<-c("BELGIUM","GERMANY","IRELAND","GREECE","SPAIN","FRANCE","ITALY",
	"LUXEMBOURG","NETHERLANDS","AUSTRIA","PORTUGAL",
	"FINLAND")

Aggregates<-list(EUaffiliates, EU27, EA17, EA12)

###################################################################################
### Sigma convergence:
### Make cross-country variances for each year and four country groups

sigma<-vector("list")

## for VaA
sigma$VaA<-sapply(1:4, function(x) apply(subset(WGI_VaA, subset= Country %in% Aggregates[[x]]),2,FUN=var, na.rm=FALSE)[-1])
years<-as.vector(c(1996,1998,2000,2002:2012),mode="integer")
sigma$VaA<-as.data.frame(sigma$VaA,row.names=years)
colnames(sigma$VaA)<-c("EUaffiliates", "EU27", "EA17", "EA12")

## for CoC
sigma$CoC<-sapply(1:4, function(x) apply(subset(WGI_CoC, subset= Country %in% Aggregates[[x]]),2,FUN=var, na.rm=FALSE)[-1])
years<-as.vector(c(1996,1998,2000,2002:2012),mode="integer")
sigma$CoC<-as.data.frame(sigma$CoC,row.names=years)
colnames(sigma$CoC)<-c("EUaffiliates", "EU27", "EA17", "EA12")

## for GE
sigma$GE<-sapply(1:4, function(x) apply(subset(WGI_GE, subset= Country %in% Aggregates[[x]]),2,FUN=var, na.rm=FALSE)[-1])
years<-as.vector(c(1996,1998,2000,2002:2012),mode="integer")
sigma$GE<-as.data.frame(sigma$GE,row.names=years)
colnames(sigma$GE)<-c("EUaffiliates", "EU27", "EA17", "EA12")

## for PSNV
sigma$PSNV<-sapply(1:4, function(x) apply(subset(WGI_PSNV, subset= Country %in% Aggregates[[x]]),2,FUN=var, na.rm=FALSE)[-1])
years<-as.vector(c(1996,1998,2000,2002:2012),mode="integer")
sigma$PSNV<-as.data.frame(sigma$PSNV,row.names=years)
colnames(sigma$PSNV)<-c("EUaffiliates", "EU27", "EA17", "EA12")

## for RoL
sigma$RoL<-sapply(1:4, function(x) apply(subset(WGI_RoL, subset= Country %in% Aggregates[[x]]),2,FUN=var, na.rm=FALSE)[-1])
years<-as.vector(c(1996,1998,2000,2002:2012),mode="integer")
sigma$RoL<-as.data.frame(sigma$RoL,row.names=years)
colnames(sigma$RoL)<-c("EUaffiliates", "EU27", "EA17", "EA12")

## for RQ
sigma$RQ<-sapply(1:4, function(x) apply(subset(WGI_RQ, subset= Country %in% Aggregates[[x]]),2,FUN=var, na.rm=FALSE)[-1])
years<-as.vector(c(1996,1998,2000,2002:2012),mode="integer")
sigma$RQ<-as.data.frame(sigma$RQ,row.names=years)
colnames(sigma$RQ)<-c("EUaffiliates", "EU27", "EA17", "EA12")

## Select sigmas for even-numbered years.
even_years<-seq(from=1996,to=2012,by=2)
sigma<-lapply(sigma, FUN=subset, subset= rownames(sigma$RQ)%in% even_years)

# Generate figures

WGInames<-c("VaA","CoC","GE","PSNV","RoL","RQ")
xmax<-vector(mode="integer")

Sub<-vector(length=6)
Sub[1]<-expression( sigma["VaA"]^2)
Sub[2]<-expression( sigma["CoC"]^2)
Sub[3]<-expression( sigma["GE"]^2)
Sub[4]<-expression( sigma["PSNV"]^2)
Sub[5]<-expression( sigma["RoL"]^2)
Sub[6]<-expression( sigma["RQ"]^2)

Main<-vector(length=6)
Main[1]<-"Voice and accountability"
Main[2]<-"Control of corruption"
Main[3]<-"Government effectiveness"
Main[4]<-"Political stability and absence of violence"
Main[5]<-"Rule of law"
Main[6]<-"Regulatory quality"


win.metafile(filename = "WGI_sigma_conv.wmf",  width = 6.3, height = 9, restoreConsole = FALSE)
par(mfrow=c(3,2),mar=c(3.5,4,2,1))

for(i in 1:6) {
 xmax[i]<-max(sigma[[i]][["EUaffiliates"]])

 plot(rownames(sigma[[i]]),sigma[[i]][["EUaffiliates"]],ylim=c(0, xmax[i]+0.05), type="l", 
	xlab="", ylab=Sub[i], cex.main=1.05, main=Main[i],
	mgp=c(2.2, 1,0))
 lines(rownames(sigma[[i]]),sigma[[i]][["EU27"]], lty=2);
 lines(rownames(sigma[[i]]),sigma[[i]][["EA17"]], type="o")
 lines(rownames(sigma[[i]]),sigma[[i]][["EA12"]], type="o", pch=4)
 legend(x="topright",legend=c("EU+aspirants","EU27", "EA17", "EA12"),
  lty=c(1,2,1,1),pch=c(NA,NA,1,4) ,bty="n")
}

dev.off()


###################################################################################
### Beta convergence:

## Calculate differences for each WGI between 2012 and 1996, except for Kosovo und Montenegro because of missing data.
for(i in 1:6) {
 WGI[[i]]["delta"]<-WGI[[i]]["Estimate.2012"] - WGI[[i]]["Estimate.1996"]
}

WGI_EUaffiliates<-lapply(WGI, FUN=subset, subset= Country %in% EUaffiliates)
WGI_EU27<-lapply(WGI, FUN=subset, subset= Country %in% EU27)
WGI_EA17<-lapply(WGI, FUN=subset, subset= Country %in% EA17)
WGI_EA12<-lapply(WGI, FUN=subset, subset= Country %in% EA12)

# define vectors and lists
z<-vector("list",6)
Sum<-vector("list",6)
r2<-vector(length=6)
Coef1<-vector(length=6)
Coef2<-vector(length=6)
Pval_Ftest<-vector(length=6)
R2<-vector(length=6)
Formula<-vector(length=6)
xsub<-vector("character")
ysub<-vector("character")

WGInames<-c("VaA","CoC","GE","PSNV","RoL","RQ")

### Generate figures: beta convergence

## in EA12
win.metafile(filename = "WGI_beta_conv_EA12.wmf",  width = 6.3, height = 9, restoreConsole = FALSE)
par(mfrow=c(3,2),mar=c(3.5,4,2,1))

for(i in 1:6) {
 xsub[i]<-paste(WGInames[i],"in 1996")
 ysub[i]<-paste("Change in",WGInames[i],"1996�2012")
 
 plot(WGI_EA12[[i]][["Estimate.1996"]],WGI_EA12[[i]][["delta"]], xlim=c(0.2,2.3) ,
	ylim=c(-0.8,0.5),  xlab=xsub[i], ylab=ysub[i], cex.main=1.05, main=Main[i],
	mgp=c(2.2, 1,0))
 z[[i]]<-lm( delta ~Estimate.1996 , data=WGI_EA12[[i]])
 abline(reg=z[[i]])
 
 Sum[[i]]<-summary(z[[i]])
 r2[i]<-getElement(Sum[[i]],"r.squared")
 r2[i]<-round(r2[i],2)
 Coef1[i]<-round(Sum[[i]][["coefficients"]][1],2)
 Coef2[i]<-round(Sum[[i]][["coefficients"]][2],2)
 Pval_Ftest[i]<-round(Sum[[i]][["coefficients"]][8],2)
 
 legend("topright", legend = bquote(paste("slope coef." == .(Coef2[i]),",  ","p-value"== .(Pval_Ftest[i]),",  ", R^2== .(r2[i]) )),bty="n")
}
dev.off()


## in EA17
win.metafile(filename = "WGI_beta_conv_EA17.wmf",  width = 6.3, height = 9, restoreConsole = FALSE)
par(mfrow=c(3,2),mar=c(3.5,4,2,1))

for(i in 1:6) {
 xsub[i]<-paste(WGInames[i],"in 1996")
 ysub[i]<-paste("Change in",WGInames[i],"1996�2012")
 
 plot(WGI_EA17[[i]][["Estimate.1996"]],WGI_EA17[[i]][["delta"]], xlim=c(0,2.5) ,
	ylim=c(-0.8,1), xlab=xsub[i], ylab=ysub[i], cex.main=1.05, main=Main[i],
	mgp=c(2.2, 1,0))
 z[[i]]<-lm( delta ~Estimate.1996 , data=WGI_EA17[[i]])
 abline(reg=z[[i]])
 
 Sum[[i]]<-summary(z[[i]])
 r2[i]<-getElement(Sum[[i]],"r.squared") 
 r2[i]<-round(r2[i],2)
 Coef1[i]<-round(Sum[[i]][["coefficients"]][1],2) 
 Coef2[i]<-round(Sum[[i]][["coefficients"]][2],2) 
 Pval_Ftest[i]<-round(Sum[[i]][["coefficients"]][8],2) 
 
 legend("topright", legend = bquote(paste("slope coef." == .(Coef2[i]),",  ","p-value"== .(Pval_Ftest[i]),",  ", R^2== .(r2[i]) )),bty="n")
}
dev.off()

## in EU27
win.metafile(filename = "WGI_beta_conv_EU27.wmf",  width = 6.3, height = 9, restoreConsole = FALSE)
par(mfrow=c(3,2),mar=c(3.5,4,2,1))

for(i in 1:6) {
 xsub[i]<-paste(WGInames[i],"in 1996")
 ysub[i]<-paste("Change in",WGInames[i],"1996�2012")
 
 plot(WGI_EU27[[i]][["Estimate.1996"]],WGI_EU27[[i]][["delta"]], xlim=c(-1,2.5) ,
	ylim=c(-0.8,1), xlab=xsub[i], ylab=ysub[i], cex.main=1.05, main=Main[i],
	mgp=c(2.2, 1,0))
 z[[i]]<-lm( delta ~Estimate.1996 , data=WGI_EU27[[i]])
 abline(reg=z[[i]])
 
 Sum[[i]]<-summary(z[[i]])
 r2[i]<-getElement(Sum[[i]],"r.squared") 
 r2[i]<-round(r2[i],2) 
 Coef1[i]<-round(Sum[[i]][["coefficients"]][1],2) 
 Coef2[i]<-round(Sum[[i]][["coefficients"]][2],2) 
 Pval_Ftest[i]<-round(Sum[[i]][["coefficients"]][8],2)
 
 legend("topright", legend = bquote(paste("slope coef." == .(Coef2[i]),",  ","p-value"== .(Pval_Ftest[i]),",  ", R^2== .(r2[i]) )),bty="n")
}
dev.off()


## in EUaffiliates
win.metafile(filename = "WGI_beta_conv_EUaffiliates.wmf",  width = 6.3, height = 9, restoreConsole = FALSE)
par(mfrow=c(3,2),mar=c(3.5,4,2,1))

for(i in 1:6) {
 xsub[i]<-paste(WGInames[i],"in 1996")
 ysub[i]<-paste("Change in",WGInames[i],"1996�2012")
 
 plot(WGI_EUaffiliates[[i]][["Estimate.1996"]],WGI_EUaffiliates[[i]][["delta"]],xlim=c(-2,2.3) ,
	ylim=c(-0.7,1.5), xlab=xsub[i], ylab=ysub[i], cex.main=1.05, main=Main[i],
	mgp=c(2.2, 1,0))
 z[[i]]<-lm( delta ~Estimate.1996 , data=WGI_EUaffiliates[[i]])
 abline(reg=z[[i]])
 
 Sum[[i]]<-summary(z[[i]])
 r2[i]<-getElement(Sum[[i]],"r.squared") 
 r2[i]<-round(r2[i],2) 
 Coef1[i]<-round(Sum[[i]][["coefficients"]][1],2) 
 Coef2[i]<-round(Sum[[i]][["coefficients"]][2],2) 
 Pval_Ftest[i]<-round(Sum[[i]][["coefficients"]][8],2) 
 
 legend("topright", legend = bquote(paste("slope coef." == .(Coef2[i]),",  ","p-value"== .(Pval_Ftest[i]),",  ", R^2== .(r2[i]) )),bty="n")
}
dev.off()








